package com.ysjy.course.controller;

import com.github.pagehelper.PageInfo;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.PromptMessage;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.UserDto;
import com.wkbb.common.utils.StringUtil;
import com.ysjy.course.model.YsTeacher;
import com.ysjy.course.service.YsTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(description = "机构教师管理")
@RequestMapping("/teacher/manage")
@Slf4j
public class YsTeacherManageController extends BaseController {

    @Autowired
    private YsTeacherService teacherService;

    @ApiOperation(value = "获取机构教师列表")
    @RequestMapping(value = "getTeacherListByPage", method = RequestMethod.GET)
    public ResultDTO<PageInfo<YsTeacher>> getTeacherListByPage(
            @ApiParam(value = "教师姓名/手机号码（模糊搜索）", required = false) @RequestParam(name = "filter", required = false) String filter,
            @ApiParam(value = "页码", required = true, defaultValue = "1") @RequestParam(name = "pageNo", required = true, defaultValue = "1") Integer pageNo,
            @ApiParam(value = "每页条数", required = true, defaultValue = "10") @RequestParam(name = "pageSize", required = true, defaultValue = "10") Integer pageSize) {
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            Map<String, Object> params = new HashMap<>();
            if (!StringUtil.isEmpty(filter)) params.put("filter", filter);
            if (userDto.getOrgId() != 1) params.put("orgId",userDto.getOrgId());
            PageInfo<YsTeacher> pageInfo = teacherService.getListByPage(params, pageNo, pageSize);
            return ResultDTO.successResult(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "获取机构教师列表（课程关联教师时加载使用）")
    @RequestMapping(value = "getTeacherList", method = RequestMethod.GET)
    public ResultDTO<List<YsTeacher>> getCourseListByPage(
            @ApiParam(value = "教师姓名/手机号码（模糊搜索）", required = false) @RequestParam(name = "filter", required = false) String filter) {
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            Map<String, Object> params = new HashMap<>();
            if (!StringUtil.isEmpty(filter)) params.put("filter", filter);
            if (userDto.getOrgId() != 1) params.put("orgId",userDto.getOrgId());
            List<YsTeacher> pageInfo = teacherService.getListByMapParams(params);
            return ResultDTO.successResult(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "获取教师详情")
    @RequestMapping(value = "getTeacherInfo", method = RequestMethod.GET)
    public ResultDTO<YsTeacher> getTeacherInfo(
            @ApiParam(value = "id", required = true) @RequestParam(name = "id", required = true) Long id) {
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            YsTeacher data = teacherService.getByPrimaryKey(id);
            if (data != null) return ResultDTO.successResult(data);
            else return ResultDTO.errorResult(PromptMessage.LOAD_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "添加 or 编辑教师",notes = "{\"realName\":\"刘志鹏\",\"nickname\":\"刘总\",\"mobile\":\"13613020696\",\"password\":\"e10adc3949ba59abbe56e057f20f883e\",\"sex\":1,\"avatar\":\"https://qiniu.childrenonline.cn/avatar/teacher_male.png\",\"season\":4,\"introduce\":\"教师简介教师简介\",\"qualificationCertificates\":\"https://qiniu.childrenonline.cn/avatar/teacher_male.png,https://qiniu.childrenonline.cn/avatar/teacher_male.png\",\"content\":\"教学成果教学成果\",\"resumeUrl\":\"https://qiniu.childrenonline.cn/avatar/teacher_male.png\",\"enabled\":true}")
    @RequestMapping(value = "putTeacher", method = RequestMethod.POST)
    public ResultDTO<Boolean> putTeacher(
            @Valid @RequestBody YsTeacher data){
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            data.setOrgId(userDto.getOrgId());
            return teacherService.putTeacher(data);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.errorResult(PromptMessage.INFO_IS_NULL);
        }
    }

    @ApiOperation(value = "删除教师")
    @RequestMapping(value = "delTeacher", method = RequestMethod.POST)
    public ResultDTO<Boolean> delTeacher(
        @ApiParam(value = "唯一标识", required = true) @RequestParam(name = "id", required = true) Long id){
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            teacherService.deleteByPrimaryKey(id);
            return new ResultDTO<>(true);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.errorResult(PromptMessage.INFO_IS_NULL);
        }
    }


}